package com.cqut.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cqut.model.ProblemManager;
import com.cqut.pojo.vo.problemInfoVO;
import com.cqut.dao.ProblemManagerMapper;
import com.cqut.dao.ProblemManagerMapperExtra;
import com.cqut.dto.LimitShowDTO;
import com.cqut.dto.ProblemManagerDTO;
import com.cqut.util.SqlUtil;
import com.cqut.service.IProblemManagerService;

@Service
public class ProblemManagerService implements IProblemManagerService {
    @Resource(name="problemManagerMapper")
    private ProblemManagerMapper problemManagerMapper;
    
    @Resource(name="problemManagerMapperExtra")
    private ProblemManagerMapperExtra problemManagerMapperExtra;

    @Override
    public boolean save(ProblemManager problemManager) {
    	String problemId = String.valueOf(System.currentTimeMillis());
    	problemManager.setProblemId(problemId);
        return problemManagerMapper.insert(problemManager) > 0;
    }

    @Override
    public boolean update(ProblemManager problemManager) {
        return problemManagerMapper.updateByPrimaryKeySelective(problemManager) >= 0;
    }

    @Override
    public boolean delete(String problemId) {
        return problemManagerMapper.deleteByPrimaryKey(problemId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return problemManagerMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public ProblemManager findModel(String problemId) {
        return problemManagerMapper.selectByPrimaryKey(problemId);
    }

    @Override
    public ProblemManager findModel(String problemId, String[] parameters) {
        return problemManagerMapper.findModel(problemId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String problemId) {
        String[][] parameters = {{"problem_id", "problemId"}, {"problem_detail", "problemDetail"}, {"programme", "programme"}};
        return this.findMap(problemId, parameters);
    }

    public Map<String, Object> findMap(String problemId, String[][] parameters) {
        return problemManagerMapper.findMap(problemId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<ProblemManager> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<ProblemManager> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<ProblemManager> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return problemManagerMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"problem_id", "problemId"}, {"problem_detail", "problemDetail"}, {"programme", "programme"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"problem_id", "problemId"}, {"problem_detail", "problemDetail"}, {"programme", "programme"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return problemManagerMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return problemManagerMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }

	@Override
	public List<problemInfoVO> loadProblemList(
			ProblemManagerDTO problemManagerDTO, LimitShowDTO limitShowDTO) {
		return problemManagerMapperExtra.findProblems(problemManagerDTO.getProblemDetail(),limitShowDTO.calculateStart(), limitShowDTO.calculateEnd());
	}

	@Override
	public Object findCountOfProblems(ProblemManagerDTO problemManagerDTO) {
		String count = 
				problemManagerMapperExtra.findProblemsNo(problemManagerDTO.getProblemDetail(), -1, -1);
		return count;
	}

}
